package com.distributed.netty.client;

import com.distributed.netty.entity.ElementJob;
import com.distributed.netty.entity.Message;
import com.distributed.netty.entity.Cache;
import com.distributed.netty.server.ChatHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import org.apache.log4j.Logger;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;

/**
 * @desc	客户端操作手柄	
 * @author	高增源
 * @time	2021年11月18日
 */
public class HeartBeatClientHandler extends ChannelInboundHandlerAdapter {
	LinkedList<ElementJob> jobList;
	Logger LOG=Logger.getLogger(ChatHandler.class);

	public HeartBeatClientHandler(LinkedList<ElementJob> jobList) {
		this.jobList=jobList;
	}

	@Override
	public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

		if (evt instanceof IdleStateEvent){
			IdleStateEvent event = (IdleStateEvent)evt;
			if (event.state()== IdleState.WRITER_IDLE){

				if (!jobList.isEmpty()){
					//回复消息
					Message replyMessage=new Message();
					//对发送来的slave记录其ip 发送job在slave端验证如果是自己则删除job
					replyMessage.setMsg("我是slave 请求更新操作");
					HashSet jobs=new HashSet();
					jobs.addAll(jobList);
					replyMessage.setElementJobs(jobs);
					ctx.writeAndFlush(replyMessage);
//					jobList.clear();
				}else{
					//回复消息
					Message replyMessage=new Message();
					replyMessage.setMsg("biubiu");
					ctx.writeAndFlush(replyMessage);
				}
			}
		}
	}

}
